/* ====================================================================
 * (#)Header: FileUtils.java
 * (#)Revision: 
 * (#)Date Created: Mar 2, 2007 12:07:22 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.io;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
 * <code>FileUtils</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class FileUtils {

	static {
		instance = new FileUtils();
	}

	private static FileUtils instance;

	private FileUtils() {
	}

	public String readFile(String fileName) {
		FileInputStream fs = null;
		try {
			fs = new FileInputStream(fileName);
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
		return toString(fs);
	}

	public String readFileFromCp(String fileName) {
		InputStream is = getClass().getResourceAsStream(fileName);
		return toString(is);
	}

	protected String toString(InputStream is) {
		StringBuilder builder = new StringBuilder();
		try {
			DataInputStream in = new DataInputStream(is);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				builder.append(strLine);
			}
			is.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return builder.toString();
	}

	public static FileUtils getInstance() {
		return instance;
	}
}
